<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<form [formGroup]="timewindowForm" (ngSubmit)="update()">
  <mat-tab-group dynamicHeight="true" [class.tb-headless]="historyOnly"
    (selectedTabChange)="onTimewindowTypeChange()" [(selectedIndex)]="timewindow.selectedTab">
    <mat-tab label="{{ 'timewindow.realtime' | translate }}">
      <section class="flex">
        <section class="flex flex-1 flex-col" [class.!hidden]="!(isEdit || !timewindow.hideInterval)">
          <div formGroupName="realtime" class="mat-content mat-padding" style="padding-top: 8px;">
            @if (!quickIntervalOnly) {
              <mat-radio-group
                [class.!hidden]="!(isEdit || (!timewindow.hideLastInterval && !timewindow.hideQuickInterval))"
                formControlName="realtimeType">
                <mat-radio-button [value]="realtimeTypes.LAST_INTERVAL" color="primary">
                  <section class="flex">
                    @if (isEdit) {
                      <section class="flex flex-col items-center justify-start" style="padding-right: 8px;">
                        <label class="tb-small hide-label" translate>timewindow.hide</label>
                        <mat-checkbox [ngModelOptions]="{standalone: true}" [(ngModel)]="timewindow.hideLastInterval"
                        (ngModelChange)="onHideLastIntervalChanged()"></mat-checkbox>
                      </section>
                    }
                    <section class="flex flex-col">
                      <span translate>timewindow.last</span>
                      @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL) {
                        <tb-timeinterval
                          formControlName="timewindowMs"
                          predefinedName="timewindow.last"
                          [required]="timewindow.selectedTab === timewindowTypes.REALTIME &&
                                timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL"
                          style="padding-top: 8px;"></tb-timeinterval>
                      }
                    </section>
                  </section>
                </mat-radio-button>
                <mat-radio-button [value]="realtimeTypes.INTERVAL" color="primary">
                  <section class="flex">
                    @if (isEdit) {
                      <section class="flex flex-col items-center justify-start" style="padding-right: 8px;">
                        <label class="tb-small hide-label" translate>timewindow.hide</label>
                        <mat-checkbox [ngModelOptions]="{standalone: true}" [(ngModel)]="timewindow.hideQuickInterval"
                        (ngModelChange)="onHideQuickIntervalChanged()"></mat-checkbox>
                      </section>
                    }
                    <section class="flex flex-col">
                      <span translate>timewindow.interval</span>
                      @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL) {
                        <tb-quick-time-interval
                          formControlName="quickInterval"
                          onlyCurrentInterval="true"
                          [required]="timewindow.selectedTab === timewindowTypes.REALTIME &&
                                timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL"
                          style="padding-top: 8px"></tb-quick-time-interval>
                      }
                    </section>
                  </section>
                </mat-radio-button>
              </mat-radio-group>
            }
            @if (!isEdit && !timewindow.hideLastInterval && timewindow.hideQuickInterval) {
              <tb-timeinterval
                formControlName="timewindowMs"
                predefinedName="timewindow.last"
                required
              style="padding-top: 8px;"></tb-timeinterval>
            }
            @if (quickIntervalOnly || !isEdit && timewindow.hideLastInterval && !timewindow.hideQuickInterval) {
              <tb-quick-time-interval
                formControlName="quickInterval"
                onlyCurrentInterval="true"
                required
              style="padding-top: 8px"></tb-quick-time-interval>
            }
          </div>
        </section>
      </section>
    </mat-tab>
    <mat-tab label="{{ 'timewindow.history' | translate }}">
      <section class="flex">
        <section class="flex flex-1 flex-col" [class.!hidden]="!(isEdit || !timewindow.hideInterval)">
          <div formGroupName="history" class="mat-content mat-padding" style="padding-top: 8px;">
            <mat-radio-group formControlName="historyType">
              @if (forAllTimeEnabled) {
                <mat-radio-button [value]="historyTypes.FOR_ALL_TIME" color="primary">
                  <section class="flex flex-col">
                    <span translate>timewindow.for-all-time</span>
                  </section>
                </mat-radio-button>
              }
              <mat-radio-button [value]="historyTypes.LAST_INTERVAL" color="primary">
                <section class="flex flex-col">
                  <span translate>timewindow.last</span>
                  @if (timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL) {
                    <tb-timeinterval
                      formControlName="timewindowMs"
                      predefinedName="timewindow.last"
                      class="history-time-input"
                      [required]="timewindow.selectedTab === timewindowTypes.HISTORY &&
                              timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL"
                      style="padding-top: 8px;"></tb-timeinterval>
                  }
                </section>
              </mat-radio-button>
              <mat-radio-button [value]="historyTypes.FIXED" color="primary">
                <section class="flex flex-col">
                  <span translate>timewindow.time-period</span>
                  @if (timewindowForm.get('history.historyType').value === historyTypes.FIXED) {
                    <tb-datetime-period
                      formControlName="fixedTimewindow"
                      class="history-time-input"
                      [required]="timewindow.selectedTab === timewindowTypes.HISTORY &&
                              timewindowForm.get('history.historyType').value === historyTypes.FIXED"
                      style="padding-top: 8px;"></tb-datetime-period>
                  }
                </section>
              </mat-radio-button>
              <mat-radio-button [value]="historyTypes.INTERVAL" color="primary">
                <section class="flex flex-col">
                  <span translate>timewindow.interval</span>
                  @if (timewindowForm.get('history.historyType').value === historyTypes.INTERVAL) {
                    <tb-quick-time-interval
                      formControlName="quickInterval"
                      class="history-time-input"
                      [required]="timewindow.selectedTab === timewindowTypes.HISTORY &&
                            timewindowForm.get('history.historyType').value === historyTypes.INTERVAL"
                      style="padding-top: 8px"></tb-quick-time-interval>
                  }
                </section>
              </mat-radio-button>
            </mat-radio-group>
          </div>
        </section>
      </section>
      <ng-container *ngTemplateOutlet="additionalData">
      </ng-container>
    </mat-tab>
  </mat-tab-group>
  <ng-template #additionalData>
    @if (aggregation) {
      <div formGroupName="aggregation" class="mat-content mat-padding flex flex-col">
        <section class="flex">
          <section class="flex flex-col items-center justify-start" [class.!hidden]="!isEdit">
            <label class="tb-small hide-label" translate>timewindow.hide</label>
            <mat-checkbox [ngModelOptions]="{standalone: true}" [(ngModel)]="timewindow.hideAggregation"
            (ngModelChange)="onHideAggregationChanged()"></mat-checkbox>
          </section>
          <section class="flex flex-1 flex-col" [class.!hidden]="!(isEdit || !timewindow.hideAggregation)">
            <mat-form-field>
              <mat-label translate>aggregation.function</mat-label>
              <mat-select formControlName="type" style="min-width: 150px;">
                @for (aggregation of aggregations; track aggregation) {
                  <mat-option [value]="aggregation">
                    {{ aggregationTypesTranslations.get(aggregationTypes[aggregation]) | translate }}
                  </mat-option>
                }
              </mat-select>
            </mat-form-field>
          </section>
        </section>
        @if (timewindowForm.get('aggregation.type').value === aggregationTypes.NONE) {
          <section class="flex">
            <section class="flex flex-col items-center justify-start" [class.!hidden]="!isEdit">
              <label class="tb-small hide-label" translate>timewindow.hide</label>
              <mat-checkbox [ngModelOptions]="{standalone: true}" [(ngModel)]="timewindow.hideAggInterval"
              (ngModelChange)="onHideAggIntervalChanged()"></mat-checkbox>
            </section>
        </section>
      }
    </div>
  }
  @if (aggregation && timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE &&
    timewindow.selectedTab === timewindowTypes.REALTIME) {
    <div formGroupName="realtime"
      class="mat-content mat-padding flex flex-col">
      <tb-timeinterval
        formControlName="interval"
        [isEdit]="isEdit"
        [(hideFlag)]="timewindow.hideAggInterval"
        (hideFlagChange)="onHideAggIntervalChanged()"
        [min]="minRealtimeAggInterval()" [max]="maxRealtimeAggInterval()"
        useCalendarIntervals
        predefinedName="aggregation.group-interval">
      </tb-timeinterval>
    </div>
  }
  @if (aggregation && timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE &&
    timewindow.selectedTab === timewindowTypes.HISTORY) {
    <div formGroupName="history"
      class="mat-content mat-padding flex flex-col">
      <tb-timeinterval
        formControlName="interval"
        [isEdit]="isEdit"
        [(hideFlag)]="timewindow.hideAggInterval"
        (hideFlagChange)="onHideAggIntervalChanged()"
        [min]="minHistoryAggInterval()" [max]="maxHistoryAggInterval()"
        predefinedName="aggregation.group-interval">
      </tb-timeinterval>
    </div>
  }
  @if (timezone) {
    <div class="mat-content mat-padding flex">
      <section class="flex flex-col items-center justify-start" [class.!hidden]="!isEdit">
        <label class="tb-small hide-label" translate>timewindow.hide</label>
        <mat-checkbox [ngModelOptions]="{standalone: true}" [(ngModel)]="timewindow.hideTimezone"
        (ngModelChange)="onHideTimezoneChanged()"></mat-checkbox>
      </section>
    </div>
  }
</ng-template>
</form>
<div class="tb-panel-actions flex flex-row items-center justify-end">
  <button type="button"
    mat-button
    [disabled]="(isLoading$ | async)"
    (click)="cancel()">
    {{ 'action.cancel' | translate }}
  </button>
  <button type="button"
    mat-raised-button
    color="primary"
    (click)="update()"
    [disabled]="(isLoading$ | async) || timewindowForm.invalid || !timewindowForm.dirty">
    {{ 'action.update' | translate }}
  </button>
</div>
